package com.ls.fw.mvc.springmvc.support.doc;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;

import com.ls.fw.commons.config.property.FwPropertyPlaceholderConfigurer;
import com.ls.fw.commons.core.utils.string.StringUtils;

public class DocketBean extends Docket implements InitializingBean {
	

	
	/**
	 * @param documentationType
	 */
	public DocketBean() {
		super(DocumentationType.SWAGGER_2);
	}
	@Autowired
	private FwPropertyPlaceholderConfigurer  propertyConfigurer;

	private String title;//api名称
	private String desc;//api描述
	private String name;//联系人
	private String url = "http://git.oschina.net/defenderhhhh";
	private String email = "2914800013@qq.com"; 
	private String version = "V1.0.0"; 
	private String group; 
	private String basePackage; 
	private boolean classAnnotationEnable = false; 
	private boolean methodAnnotationEnable = false; 
	
	/**
	 * 哪些路径可以生成文档
	 */
	private String path = "/**"; 
	
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUrl() {
		return this.url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getEmail() {
		return this.email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
    private ApiInfo apiInfo() {
    	 Contact contact = new Contact(name, url, email);
         return new ApiInfoBuilder()
                .title(title)
                .description(desc)
                .contact(contact)
                .license("Apache 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .version(version)
                .build();
    }

    public FwPropertyPlaceholderConfigurer getPropertyConfigurer() {
		return this.propertyConfigurer;
	}
	public void setPropertyConfigurer(
			FwPropertyPlaceholderConfigurer propertyConfigurer) {
		this.propertyConfigurer = propertyConfigurer;
	}
	public String getDesc() {
		return this.desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public String getVersion() {
		return this.version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
	public String getGroup() {
		return this.group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	public String getPath() {
		return this.path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		if(StringUtils.isBlank(title)){
			title = this.propertyConfigurer.getProperty("systemName");
		}
		if(StringUtils.isBlank(name)){
			name = this.propertyConfigurer.getProperty("author");
		}
		this.groupName(group)
        //.genericModelSubstitutes(ResponseEntity.class)
        .useDefaultResponseMessages(false)
        .forCodeGeneration(true);
    	
    	ApiSelectorBuilder ab = this.select();
    	if(StringUtils.isNotBlank(basePackage)){
    		ab.apis(RequestHandlerSelectors.basePackage(basePackage));
    	}
    	if(classAnnotationEnable){
    		ab.apis(RequestHandlerSelectors.withClassAnnotation(Api.class));
    	}
    	if(methodAnnotationEnable){
    		ab.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
    	}
    	//ab.paths(PathSelectors.any())// 对所有路径进行监控
        ab.paths(PathSelectors.ant(path))//过滤的接口
        ;
        ab.build().apiInfo(apiInfo()); 
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getBasePackage() {
		return this.basePackage;
	}
	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}
	public boolean isClassAnnotationEnable() {
		return this.classAnnotationEnable;
	}
	public void setClassAnnotationEnable(boolean classAnnotationEnable) {
		this.classAnnotationEnable = classAnnotationEnable;
	}
	public boolean isMethodAnnotationEnable() {
		return this.methodAnnotationEnable;
	}
	public void setMethodAnnotationEnable(boolean methodAnnotationEnable) {
		this.methodAnnotationEnable = methodAnnotationEnable;
	}
}
